load("//tools/build_defs/android:fb_xplat_android_cxx_library.bzl", "fb_xplat_android_cxx_library")
load("//tools/build_defs/oss:profilo_defs.bzl", "profilo_maybe_hidden_visibility", "profilo_path")
load(":build_defs.bzl", "unwindc_tracer_library")

unwindc_tracer_library(
    allow_64bit = True,
    version = "5.0.0",
)

unwindc_tracer_library(
    allow_64bit = True,
    version = "5.1.0",
)

unwindc_tracer_library(
    allow_64bit = True,
    version = "6.0.0",
)

unwindc_tracer_library(
    allow_64bit = True,
    version = "7.0.0",
)

unwindc_tracer_library(
    allow_64bit = True,
    version = "7.1.0",
)

unwindc_tracer_library(
    allow_64bit = True,
    version = "7.1.1",
)

unwindc_tracer_library(version = "7.1.2")

unwindc_tracer_library(
    allow_64bit = True,
    version = "8.0.0",
)

unwindc_tracer_library(
    allow_64bit = True,
    version = "8.1.0",
)

unwindc_tracer_library(
    allow_64bit = True,
    version = "9.0.0",
)

fb_xplat_android_cxx_library(
    name = "constants",
    header_namespace = "profilo/profiler",
    exported_headers = [
        "Constants.h",
    ],
    labels = [],
    visibility = [
        profilo_path("cpp/writer/..."),
        profilo_path("cpp/test/..."),
        profilo_path("facebook/cpp/profiler/..."),
        profilo_path("facebook/cpp/unwinder/..."),
    ],
)

fb_xplat_android_cxx_library(
    name = "artcompat",
    srcs = [
        "ArtCompatibility.cpp",
        "ArtCompatibilityRunner.cpp",
    ],
    headers = [
        "ArtCompatibility.h",
        "ArtCompatibilityRunner.h",
    ],
    header_namespace = "profilo/profiler",
    exported_headers = [
        "ArtCompatibility.h",
    ],
    allow_jni_merging = True,
    compiler_flags = [
        "-fexceptions",
        "-frtti",
        "-std=gnu++17",
        "-DLOG_TAG=\"Profilo/ArtCompatibility\"",
        "-Wno-reorder-init-list",
        "-Wno-unknown-warning-option",
        "-Wno-unused-variable",
        #"-DFBLOG_NDEBUG=0",  # verbose logging
    ],
    force_static = True,
    labels = [],
    soname = "libprofilo_stacktrace_artcompat.so",
    deps = [
        ":base_tracer",
        ":signal_handler",
        ":unwindc-tracer-5.0.0",
        ":unwindc-tracer-5.1.0",
        ":unwindc-tracer-6.0.0",
        ":unwindc-tracer-7.0.0",
        ":unwindc-tracer-7.1.0",
        ":unwindc-tracer-7.1.1",
        ":unwindc-tracer-7.1.2",
        ":unwindc-tracer-8.0.0",
        ":unwindc-tracer-8.1.0",
        ":unwindc-tracer-9.0.0",
        profilo_path("deps/fb:fb"),
        profilo_path("deps/fbjni:fbjni"),
        profilo_path("cpp/logger/buffer:buffer"),
        profilo_path("cpp/util:util"),
    ],
)

fb_xplat_android_cxx_library(
    name = "base_tracer",
    header_namespace = "profilo/profiler",
    exported_headers = [
        "BaseTracer.h",
        "JavaBaseTracer.h",
    ],
    labels = [],
    visibility = [
        "PUBLIC",
    ],
    deps = [
        profilo_path("cpp/api:external_api_header"),
    ],
    exported_deps = [
        profilo_path("cpp/logger:multi_buffer_logger"),
    ],
)

fb_xplat_android_cxx_library(
    name = "external_tracer",
    srcs = [
        "ExternalTracer.cpp",
    ],
    header_namespace = "profilo/profiler",
    exported_headers = [
        "ExternalTracer.h",
    ],
    force_static = True,
    labels = [],
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":base_tracer",
        profilo_path("cpp/api:external_api"),
    ],
)

fb_xplat_android_cxx_library(
    name = "external_tracer_manager",
    srcs = [
        "ExternalTracerManager.cpp",
    ],
    header_namespace = "profilo/profiler",
    exported_headers = [
        "ExternalTracerManager.h",
    ],
    labels = [],
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":external_tracer",
        profilo_path("cpp/api:external_api"),
    ],
)

fb_xplat_android_cxx_library(
    name = "js_tracer",
    srcs = [
        "JSTracer.cpp",
    ],
    header_namespace = "profilo/profiler",
    exported_headers = [
        "JSTracer.h",
    ],
    force_static = True,
    labels = [],
    preprocessor_flags = [
        "-DLOG_TAG=\"Profilo/JS\"",
    ],
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":external_tracer",
        profilo_path("deps/atomic:atomic"),
        profilo_path("cpp/logger/buffer:buffer"),
    ],
)

fb_xplat_android_cxx_library(
    name = "tracer_utils",
    srcs = [
        "DalvikUtils.cpp",
    ],
    header_namespace = "profilo/profiler",
    exported_headers = [
        "DalvikUtils.h",
    ],
    force_static = True,
    labels = [],
    visibility = [
        "PUBLIC",
    ],
    deps = [
        profilo_path("deps/dalvik:dalvik-subset-headers-for-tracer"),
    ],
)

fb_xplat_android_cxx_library(
    name = "dalvik_tracer",
    srcs = [
        "DalvikTracer.cpp",
    ],
    header_namespace = "profilo/profiler",
    exported_headers = [
        "DalvikTracer.h",
    ],
    compiler_flags = [
        "-fexceptions",
        "-frtti",
        "-std=gnu++17",
        "-Wno-reorder-init-list",
        "-Wno-unknown-warning-option",
    ],
    force_static = True,
    labels = [],
    preprocessor_flags = [
        "-DLOG_TAG=\"Profilo/Dalvik\"",
    ],
    soname = "libprofilo_dalvik_tracer.so",
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":base_tracer",
        ":tracer_utils",
        profilo_path("cpp/logger/buffer:buffer"),
    ],
)

fb_xplat_android_cxx_library(
    name = "signal_handler",
    srcs = [
        "SignalHandler.cpp",
    ],
    header_namespace = "profilo/profiler",
    exported_headers = [
        "SignalHandler.h",
    ],
    compiler_flags = [
        "-fexceptions",
        "-frtti",
        "-std=gnu++17",
        "-Wno-reorder-init-list",
        "-Wno-unknown-warning-option",
    ],
    force_static = True,
    labels = [],
    preprocessor_flags = [
        "-DLOG_TAG=\"Profilo/Signal\"",
    ],
    soname = "libprofilo_signal_handler.so",
    visibility = [
        "PUBLIC",
    ],
    deps = [
        profilo_path("deps/breakpad:abort-with-reason"),
    ],
    exported_deps = [
        profilo_path("deps/sigmux:phaser"),
    ],
)

fb_xplat_android_cxx_library(
    name = "retcode",
    srcs = [
        "Retcode.cpp",
    ],
    header_namespace = "profilo/profiler",
    exported_headers = [
        "Retcode.h",
    ],
    force_static = True,
    labels = [],
    visibility = [
        "PUBLIC",
    ],
    deps = [
        profilo_path("cpp/api:external_api_header"),
        profilo_path("cpp/logger:multi_buffer_logger"),
    ],
)

PROFILER_SRCS = [
    "SamplingProfiler.cpp",
    "ThreadTimer.cpp",
    "TimerManager.cpp",
    "jni.cpp",
]

PROFILER_HEADER_NAMESPACE = "profilo/profiler"

PROFILER_EXPORTED_HEADERS = [
    "SamplingProfiler.h",
    "ThreadTimer.h",
    "TimerManager.h",
]

PROFILER_ALLOW_JNI_MERGING = True

PROFILER_COMPILER_FLAGS = [
    "-fexceptions",
    "-frtti",
    "-DLOG_TAG=\"Profilo/Profiler\"",
    "-Wno-format-security",
    "-Wno-unknown-attributes",
    #"-DFBLOG_NDEBUG=0",  # verbose logging
    "-Wno-reorder-init-list",
    "-std=gnu++17",
    "-Wno-unknown-warning-option",
] + profilo_maybe_hidden_visibility()

PROFILER_SONAME = "libprofilo_stacktrace.$(ext)"

PROFILER_SUPPORTED_PLATFORMS_REGEX = "^(android-|linux|gcc|platform)"

PROFILER_TESTS = [
    profilo_path("cpp/test:sampling_profiler"),
]

PROFILER_BASE_DEPS = [
    ":artcompat",
    ":dalvik_tracer",
    ":external_tracer",
    ":external_tracer_manager",
    ":js_tracer",
    ":retcode",
    ":signal_handler",
    ":unwindc-tracer-5.0.0",
    ":unwindc-tracer-5.1.0",
    ":unwindc-tracer-6.0.0",
    ":unwindc-tracer-7.0.0",
    ":unwindc-tracer-7.1.0",
    ":unwindc-tracer-7.1.1",
    ":unwindc-tracer-7.1.2",
    ":unwindc-tracer-8.0.0",
    ":unwindc-tracer-8.1.0",
    ":unwindc-tracer-9.0.0",
    profilo_path("cpp:constants"),
    profilo_path("cpp:profilo"),
    profilo_path("cpp:providers"),
    profilo_path("cpp/api:external_api"),
    profilo_path("cpp/api:external_api_header"),
    profilo_path("cpp/jni:jmulti_buffer_logger"),
    profilo_path("cpp/logger/buffer:buffer"),
    profilo_path("cpp/util:util"),
    profilo_path("deps/breakpad:abort-with-reason"),
    profilo_path("deps/dalvik:dalvik-subset-headers"),
    profilo_path("deps/fbjni:fbjni"),
]

PROFILER_EXPORTED_DEPS = [
    ":base_tracer",
    ":constants",
    profilo_path("cpp/api:external_api_glue"),
    profilo_path("cpp/logger:multi_buffer_logger"),
    profilo_path("deps/fbjni:fbjni"),
]

fb_xplat_android_cxx_library(
    name = "profiler",
    srcs = PROFILER_SRCS,
    header_namespace = PROFILER_HEADER_NAMESPACE,
    exported_headers = PROFILER_EXPORTED_HEADERS,
    allow_jni_merging = PROFILER_ALLOW_JNI_MERGING,
    compiler_flags = PROFILER_COMPILER_FLAGS,
    labels = [],
    soname = PROFILER_SONAME,
    supported_platforms_regex = PROFILER_SUPPORTED_PLATFORMS_REGEX,
    tests = PROFILER_TESTS,
    visibility = [
        "PUBLIC",
    ],
    deps = PROFILER_BASE_DEPS + [
        profilo_path("deps/profiler:native_tracer"),
    ],
    exported_deps = PROFILER_EXPORTED_DEPS,
)

fb_xplat_android_cxx_library(
    name = "profiler_no_native_tracer",
    srcs = PROFILER_SRCS,
    header_namespace = PROFILER_HEADER_NAMESPACE,
    exported_headers = PROFILER_EXPORTED_HEADERS,
    allow_jni_merging = PROFILER_ALLOW_JNI_MERGING,
    compiler_flags = PROFILER_COMPILER_FLAGS,
    labels = [],
    soname = PROFILER_SONAME,
    supported_platforms_regex = PROFILER_SUPPORTED_PLATFORMS_REGEX,
    tests = PROFILER_TESTS,
    visibility = [
        "PUBLIC",
    ],
    deps = PROFILER_BASE_DEPS,
    exported_deps = PROFILER_EXPORTED_DEPS,
)
